package com.bbx.taxi.client.widget.Dailog;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Msgs;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Order.OrderCancelReason;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.Bean.Message.Publisher;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;
import com.bbx.taxi.client.Util.IsSnQiangDan;
import com.bbx.taxi.client.Util.ToCity2;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 客服改派订单，司机取消订单变成改派订单，，客服取消订单
 * 
 */
public class MyTypeDailog extends Dialog implements
		android.view.View.OnClickListener {
	public static final int TYPE_GAIPAI = 0;
	public static final int TYPE_CANCEL = 1;

	public interface onClickLeftListener {
		public void onClickLeft();
	}

	public interface onClickRightListener {
		public void onClickRight();
	}

	public interface onClickSingleListener {
		public void onClickSingle();
	}

	public onClickLeftListener monClickLeftListener;
	public onClickRightListener monClickRightListener;
	public onClickSingleListener mOnClickSingleListener;
	public LinearLayout layout_dailog;
	private LinearLayout layout_double;
	private Button btn_single;
	private TextView tv_title;
	private TextView tv_content1, tv_content2, tv_content3;
	private Button btn_cencel, btn_confirm;
	private boolean cancelable = true;
	private Context context;
	private boolean isFinish = false;
	private MyTypeDailog mMyTypeDailog;

	public MyTypeDailog(Context context) {
		super(context,(MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0)==0?R.style.Dialog_bocop:R.style.Dialog_bocop2);
		this.context = context;
		init();
	}

	private void init() {
		mMyTypeDailog = this;
		View contentView = View.inflate(getContext(), R.layout.dialog_type,
				null);
		setContentView(contentView);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_content1 = (TextView) findViewById(R.id.tv_content1);
		tv_content2 = (TextView) findViewById(R.id.tv_content2);
		tv_content3 = (TextView) findViewById(R.id.tv_content3);
		btn_cencel = (Button) findViewById(R.id.btn_cencel);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		layout_double = (LinearLayout) findViewById(R.id.layout_double);
		layout_dailog = (LinearLayout) findViewById(R.id.layout_dailog);
		btn_single = (Button) findViewById(R.id.btn_single);
		btn_cencel.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		btn_single.setOnClickListener(this);
		layout_dailog.setVisibility(View.GONE);
		this.setCancelable(false);
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
	}

	@Override
	public void show() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if(MessageDialog.map_dialog!=null){
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		MessageDialog.map_dialog.put(context.toString(), size+1);
		if(MessageDialog.map_dialog.get(context.toString())!=null&&MessageDialog.map_dialog.get(context.toString())<=MessageDialog.max_dialog){
			super.show();
			Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_in);
			layout_dailog.startAnimation(a);
			layout_dailog.setVisibility(View.VISIBLE);
			isFinish=false;
		}else{
			MessageDialog.map_dialog.put(context.toString(), MessageDialog.map_dialog.get(context.toString())-1);
		}
		}
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if (isFinish)
			super.dismiss();
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		if(size>0){
			MessageDialog.map_dialog.put(context.toString(), size-1);
		}
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_out);
		layout_dailog.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation anim) {
			}

			@Override
			public void onAnimationRepeat(Animation anim) {
			}

			@Override
			public void onAnimationEnd(Animation anim) {
				isFinish = true;
				dismiss();
			}
		});
		}

	}

	/**
	 * 设置单按钮没收
	 */
	public void setSingle(String text) {
		layout_double.setVisibility(View.GONE);
		btn_single.setVisibility(View.VISIBLE);
		btn_single.setText(text);
	}

	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}

	public void setTitle(String text) {
		if (text == null) {
			tv_title.setVisibility(View.GONE);
		} else {
			tv_title.setVisibility(View.VISIBLE);
			tv_title.setText(text);
		}

	}

	public void setContent1(String text) {
		if (text == null||text.equals("")) {
			tv_content1.setVisibility(View.GONE);
		} else {
			tv_content1.setVisibility(View.VISIBLE);
			tv_content1.setText(text);
		}

	}

	public void setContent2(String text) {
		if (text == null||text.equals("")) {
			tv_content2.setVisibility(View.GONE);
		} else {
			tv_content2.setVisibility(View.VISIBLE);
			tv_content2.setText(text);
		}

	}

	public void setContent3(String text) {
		if (text == null||text.equals("")) {
			tv_content3.setVisibility(View.GONE);
		} else {
			tv_content3.setVisibility(View.VISIBLE);
			tv_content3.setText(text);
		}

	}

	public void setLeftButtonText(String text) {
		btn_cencel.setText(text);
	}

	public void setRightButtonText(String text) {
		btn_confirm.setText(text);
	}

	public void setOnClickLeftListener(onClickLeftListener honClickLeftListener) {
		monClickLeftListener = honClickLeftListener;
	}

	public void setOnClickRightListener(
			onClickRightListener honClickRightListener) {
		monClickRightListener = honClickRightListener;
	}

	public void setOnClickSingleListener(
			onClickSingleListener hOnClickSingleListener) {
		mOnClickSingleListener = hOnClickSingleListener;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_cencel:
			dismiss();
			if (monClickLeftListener != null)
				monClickLeftListener.onClickLeft();
			break;
		case R.id.btn_confirm:
			dismiss();
			if (monClickRightListener != null)
				monClickRightListener.onClickRight();
			break;
		case R.id.btn_single:
			dismiss();
			if (mOnClickSingleListener != null)
				mOnClickSingleListener.onClickSingle();
			break;
		}

	}

	public void setModel(int type, Msgs.Data msgs_data) {
		   MyOrderList mMyOrderList = null;
		 MyOrderingDB db_myordering = new MyOrderingDB();// 我的进行中的订单数据库
		 MyOrderDB db_myorder = new MyOrderDB();// 我的进行中的订单数据库
		if (msgs_data != null) {
			switch (type) {
			case TYPE_GAIPAI:
				mMyOrderList = db_myordering.onReadDB_ing(
						msgs_data.getOrder_id());
				if (mMyOrderList != null&&mMyOrderList.locations!=null&&mMyOrderList.getAppoint_time()!=null) {	 
				}else{
					mMyOrderList = db_myorder.onReadDB(
							msgs_data.getOrder_id());
				}
			    final MyOrderList gaipai_MyOrderList=mMyOrderList;
				if (mMyOrderList != null&&mMyOrderList.locations!=null&&mMyOrderList.getAppoint_time()!=null) {
					String start_city = ToCity2.getCity(mMyOrderList
							.getLocations().getStart().getAddress());
					String end_city = ToCity2.getCity(mMyOrderList
							.getLocations().getEnd().getAddress());
                    if(msgs_data.getReason()!=null&&!msgs_data.getReason().equals("")){
					   setContent1(String.format(
							context.getString(R.string.reason),
							msgs_data.getReason()));
                    }else{
						setContent1(null);
					}
					setContent2(DateFormat.onDateRemove_ss(mMyOrderList
							.getAppoint_time()));
					setContent3(start_city + context.getString(R.string.to)+ end_city);
					setLeftButtonText(context
							.getString(R.string.mydailog_no_see));
					setRightButtonText(context
							.getString(R.string.mydailog_go_see));
					show();
				setTitle(context.getString(R.string.gaipai_order));
				setOnClickLeftListener(new onClickLeftListener() {
					@Override
					public void onClickLeft() {
						mMyTypeDailog.cancel();
					}
				});
				setOnClickRightListener(new onClickRightListener() {
					@Override
					public void onClickRight() {
						mMyTypeDailog.cancel();
						Intent intent = new Intent(context,
								StartServiceActivity.class);
						intent.putExtra(OrderMsg.extra_type, 1);
						intent.putExtra(OrderMsg.extra_order_id,
								gaipai_MyOrderList.getOrder_id());
						context.startActivity(intent);
					}
				});
				}
				break;
			case TYPE_CANCEL:
				mMyOrderList = db_myordering.onReadDB_ing(
						msgs_data.getOrder_id());
				if (mMyOrderList != null&&mMyOrderList.locations!=null&&mMyOrderList.getAppoint_time()!=null) {
					
				}else{
					 mMyOrderList = db_myorder.onReadDB(
								msgs_data.getOrder_id());
				}
				 final MyOrderList cancel_MyOrderList=mMyOrderList;
				if (mMyOrderList != null&&mMyOrderList.locations!=null&&mMyOrderList.getAppoint_time()!=null) {
					String start_city = ToCity2.getCity(mMyOrderList
							.getLocations().getStart().getAddress());
					String end_city = ToCity2.getCity(mMyOrderList
							.getLocations().getEnd().getAddress());
					if(msgs_data.getReason()!=null&&!msgs_data.getReason().equals("")){
					    setContent1(String.format(
							context.getString(R.string.reason),
							msgs_data.getReason()));
					}else{
						setContent1(null);
					}
					setContent2(DateFormat.onDateRemove_ss(mMyOrderList
							.getAppoint_time()));
					setContent3(start_city + context.getString(R.string.to) + end_city);
					setLeftButtonText(context
							.getString(R.string.mydailog_no_see));
					setRightButtonText(context
							.getString(R.string.mydailog_go_see));
					show();
					setTitle(context.getString(R.string.cancel_order));
					setOnClickLeftListener(new onClickLeftListener() {
						@Override
						public void onClickLeft() {
							mMyTypeDailog.cancel();
						}
					});
					setOnClickRightListener(new onClickRightListener() {
						@Override
						public void onClickRight() {
							mMyTypeDailog.cancel();
							Intent intent = new Intent(context,
									OrderCancelReason.class);
							intent.putExtra(OrderMsg.extra_order_id,
									cancel_MyOrderList.getOrder_id());
							context.startActivity(intent);
						}
					});
				}
				break;				
			}
			if(IsSnQiangDan.isIncludeSnQiangDanLocal(context)!=null){
				setSingle(btn_cencel.getText().toString());
			}
		}
	}
}
